package cn.edu.scau.cmi.liangzaoqing.fxml;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import cn.edu.scau.cmi.liangzaoqing.util.PropertiesUtil;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

public class CopyFileController implements Initializable {

//	private File sourceDirectory;
//	private String sourceProjectPath;
	
	private File distinationDirectory;

	private File workspaceDirectory;

//	@FXML
//	private TextField sourceTextField;

	@FXML
	private ChoiceBox<String> projectChoiceBox;

	@FXML
	private TextField distinationTextField;

	@FXML
	private TextField workspaceTextField;

	@FXML
	private Button distinationDirectoryButton;

	@FXML
	private Button workspaceButton;

	@FXML
	private Button exitPlatformButton;
	
	@FXML
	private Button nextButton;

	@Override
	public void initialize(URL arg0, ResourceBundle arg1) {
		// TODO Auto-generated method stub
//		sourceDirectory=null;
		distinationDirectory = null;
		workspaceDirectory = new File(PropertiesUtil.getValueByKey("property.properties", "workspace"));
//		sourceTextField.setEditable(false);
		distinationTextField.setEditable(false);
		workspaceTextField.setEditable(false);
		workspaceTextField.setText(PropertiesUtil.getValueByKey("property.properties", "workspace"));
		String workspacePath = workspaceTextField.getText();
		choiceBoxInit(workspacePath);
	}

	public void choiceBoxInit(String workspacePath) {
		if(workspacePath!="") {
			projectChoiceBox.getItems().clear();
			ObservableList<String> projectOptions = FXCollections.observableArrayList();
//			String path = System.getProperty("user.dir");
//			path = path.substring(0, path.lastIndexOf("\\"));
			// System.out.println(path);
			File defaultDirectory = new File(workspacePath);
			if (defaultDirectory.isDirectory()) {
				for (File file : defaultDirectory.listFiles()) {
					if (file.isDirectory() && !file.getName().startsWith(".")) {
						projectOptions.add(file.getName());
					}
				}
			}
			projectChoiceBox.setItems(projectOptions); // this statement adds all values in choiceBox
			projectChoiceBox.setValue(projectOptions.get(0));
		}
	}
//	public void browserSourceDirectoryPath() {
//		DirectoryChooser directoryChooser = new DirectoryChooser();
//		directoryChooser.setTitle("请选择需要复制的文件夹");
//		String path = System.getProperty("user.dir");
//		path = path.substring(0, path.lastIndexOf("\\"));
//		//System.out.println(path);
//		File defaultDirectory = new File(path);
//		directoryChooser.setInitialDirectory(defaultDirectory);
////		fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.xml"));
//		sourceDirectory = directoryChooser.showDialog(sourceDirectoryButton.getScene().getWindow());
//		if(sourceDirectory.exists()) {
//			sourceTextField.setText(sourceDirectory.getAbsolutePath());
//		}else {
//			System.out.println("no directory was selected!");
//		}		
//	}

	public void browserWorkspacePath() throws IOException {
		DirectoryChooser directoryChooser = new DirectoryChooser();
		directoryChooser.setTitle("请选择项目工作区");
		String path = System.getProperty("user.dir");
		path = path.substring(0, path.lastIndexOf("\\"));
		// System.out.println(path);
		File defaultDirectory = new File(path);
		directoryChooser.setInitialDirectory(defaultDirectory);
//	fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.xml"));
		workspaceDirectory = directoryChooser.showDialog(workspaceButton.getScene().getWindow());
		if (workspaceDirectory.exists()) {
			workspaceTextField.setText(workspaceDirectory.getAbsolutePath());
			PropertiesUtil.writeProperties("property.properties", "workspace", workspaceDirectory.getAbsolutePath());
			String workspacePath = workspaceTextField.getText();
			choiceBoxInit(workspacePath);
		} else {
			System.out.println("no directory was selected!");
		}
	}

	public void browserDistinationPath() {
		DirectoryChooser directoryChooser = new DirectoryChooser();
		directoryChooser.setTitle("请选择目标项目文件夹");
		String path = System.getProperty("user.dir");
		path = path.substring(0, path.lastIndexOf("\\"));
		// System.out.println(path);
		File defaultDirectory = new File(path);
		directoryChooser.setInitialDirectory(defaultDirectory);
//		fileChooser.getExtensionFilters().add(new ExtensionFilter("Text Files", "*.xml"));
		distinationDirectory = directoryChooser.showDialog(distinationDirectoryButton.getScene().getWindow());
		if (distinationDirectory.exists()) {
			distinationTextField.setText(distinationDirectory.getAbsolutePath());
		} else {
			System.out.println("no directory was selected!");
		}
	}

	public void exitPlatform() {
		Platform.exit();
	}
	
	public void nextStage() throws IOException {
		
		Stage nextStage = new Stage();
		nextStage.setTitle("文件复制应用");
		FXMLLoader loader = new FXMLLoader(getClass().getResource("FileTreePage.fxml"));
		Pane nextPane = loader.load();

		FileTreeController fileTreeController = (FileTreeController) loader.getController();
//		String path = System.getProperty("user.dir");
//		path = path.substring(0, path.lastIndexOf("\\"));
		fileTreeController.setSourceProjectPath(workspaceDirectory.getAbsolutePath() + "\\" + projectChoiceBox.getValue());
		System.out.println("选中的项目所在路径：" + workspaceDirectory.getAbsolutePath() + "\\" + projectChoiceBox.getValue());
		fileTreeController.setDistinationProjectPath(distinationTextField.getText());
		fileTreeController.initTreeView();
		Scene nextScene = new Scene(nextPane);
		nextStage.setScene(nextScene);
		nextStage.setResizable(false);
		nextStage.show();
	}
}
